package cn.yuitstyle.license.server.controller;

import cn.hutool.core.util.StrUtil;
import cn.yuitstyle.license.server.manage.LicenseCreator;
import cn.yuitstyle.license.server.model.LicenseCreatorParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于生成证书文件，不能部署在用户的系统里
 */
@RestController
@RequestMapping("/license")
public class LicenseCreatorController {

    /**
     * 证书生成路径
     */
    @Value("${license.license-path}")
    private String licensePath;


    /**
     * 生成证书
     */
    @RequestMapping(value = "/generateLicense")
    public Map<String,Object> generateLicense(@RequestBody() LicenseCreatorParam param) {
        Map<String,Object> resultMap = new HashMap<>(2);

        if(StrUtil.isBlank(param.getLicensePath())){
            param.setLicensePath(licensePath);
        }

        LicenseCreator licenseCreator = new LicenseCreator(param);
        boolean result = licenseCreator.generateLicense();

        if(result){
            resultMap.put("result","ok");
            resultMap.put("msg",param);
        }else{
            resultMap.put("result","error");
            resultMap.put("msg","证书文件生成失败！");
        }

        return resultMap;
    }
}
